//
//  MemberItemView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/19.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class MemberItemView: UIView {
    
    // 图片相关
    private let imgs = ["member1","member2","member3"]
    // 回调
    public var comoplete:((_ item:Int) -> Void)?
    
    public var memberZones:[MemberZoneInfo]? {
        didSet {
            updateLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        init_view()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var submembers = [SubMemberItemView]()
    
    private func init_view() -> Void {
        (0..<imgs.count).forEach { (i) in
            submembers.append(SubMemberItemView())
        }
    }
    
    override func layoutSubviews() {
        if submembers.count == 0 {
            return
        }
        // 高度
        var tempHeight:CGFloat = 0
        // 摆放
        for i in 0..<imgs.count {
            let subMember = submembers[i]
            subMember.tag = i
            subMember.picname = imgs[i]
            if let zones = memberZones, zones.count > 0{
                subMember.subMemberZone = zones[i]
            }
            subMember.frame = CGRect(x: 0, y: tempHeight, width: frame.width, height: MARGIN70)
            addSubview(subMember)
            // 点击事件
            subMember.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickMemberItem(ui:))))
            // 下划线
            let lineY = subMember.frame.maxY + MARGIN
            let line = UIView(frame: CGRect(x: MARGIN, y: lineY, width: frame.width - MARGIN * 2, height: 1))
            if i != imgs.count - 1 {
                line.backgroundColor = COLOR_LINE
                addSubview(line)
            }
            // 记录高度
            tempHeight = line.frame.maxY + MARGIN
        }
    }
    
    @objc func clickMemberItem(ui:UITapGestureRecognizer) -> Void {
        if let callback = comoplete,let index = ui.view?.tag {
            callback(index)
        }
    }
    
    class SubMemberItemView: UIView {
        
        // 图片
                   private let img = UIImageView()
        // 标题
        private let titleLabel = UILabel(title: "", color: UIColor.black, size: 15)
        // 日期
        private let dateLabel = UILabel(title: "", color: UIColor.gray, size: 12)
        
        public var picname:String? {
            didSet {
                img.image = UIImage(named: picname!)
            }
        }
        
        public var subMemberZone:MemberZoneInfo? {
            didSet {
                titleLabel.text = subMemberZone?.activitydes
                dateLabel.text = "活动时间：\(subMemberZone?.startdate ?? "") 至 \(subMemberZone?.stopdate ?? "")"
            }
        }
        
        override func layoutSubviews() {
            // 图片
            img.backgroundColor = UIColor.red
            img.frame = CGRect(x: MARGIN, y: 0, width: MARGIN110, height: MARGIN70)
            img.contentMode = .scaleToFill
            img.layer.cornerRadius = 5
            img.layer.masksToBounds = true
            addSubview(img)
            // 标题
            let titleX = img.frame.maxX + MARGIN10
            let titleW = frame.width - titleX - MARGIN
            titleLabel.frame = CGRect(x: titleX, y: MARGIN8, width: titleW, height: MARGIN20)
            addSubview(titleLabel)
            // 日期
            dateLabel.frame = CGRect(x: titleX, y: MARGIN50, width: titleW, height: MARGIN)
            addSubview(dateLabel)
        }
    }
}
